﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections;
using XchangeStreamer.OutputSdk;

namespace WinFormsClient
{
    public partial class Form1 : Form
    {
        private Connector connector;
        private SynchronizationContext synchronizationContext;
        
        public Form1()
        {
            InitializeComponent();
            this.Closing += new CancelEventHandler(Form1_Closing);
            this.synchronizationContext = SynchronizationContext.Current;
            this.bids.AutoGenerateColumns = false;
            this.asks.AutoGenerateColumns = false;
            this.trades.AutoGenerateColumns = false;

            //DataGridViewTextBoxColumn price = new DataGridViewTextBoxColumn();
            //price.DataPropertyName = "Price";
            //price.HeaderText = "Price";
            //price.ReadOnly = true;
            //price.ValueType = typeof(decimal);
            //price.SortMode = DataGridViewColumnSortMode.Programmatic;

            //DataGridViewTextBoxColumn quantity = new DataGridViewTextBoxColumn();
            //quantity.DataPropertyName = "Quantity";
            //quantity.HeaderText = "Quantity";
            //quantity.ReadOnly = true;
            //quantity.ValueType = typeof(uint);
            //quantity.SortMode = DataGridViewColumnSortMode.Programmatic;

            //DataGridViewTextBoxColumn splits = new DataGridViewTextBoxColumn();
            //splits.DataPropertyName = "Splits";
            //splits.HeaderText = "Splits";
            //splits.ReadOnly = true;
            //splits.ValueType = typeof(decimal);
            //splits.SortMode = DataGridViewColumnSortMode.Programmatic;

            //this.bids.Columns.AddRange(price, quantity, splits);

            //this.bids_Price.ValueType = typeof(decimal);
            //this.bids_Quantity.ValueType = typeof(uint);
            //this.bids_Splits.ValueType = typeof(decimal);

            //this.asks_Price.ValueType = typeof(decimal);
            //this.asks_Quantity.ValueType = typeof(uint);
            //this.asks_Splits.ValueType = typeof(decimal);

            Task.Factory.StartNew(this.Connect);
        }

        private void Post(SendOrPostCallback action, object state)
        {
            this.synchronizationContext.Post(action, state);
        }

        private void Send(SendOrPostCallback action, object state)
        {
            this.synchronizationContext.Send(action, state);
        }

        void Form1_Closing(object sender, CancelEventArgs e)
        {
            this.connector.Disconnect();
            this.connector.Dispose();
        }

        private void Connect()
        {
            this.connector = Connector.Connection;
            this.connector.Connect();

            this.Post(this.Bind, null);
        }

        private void Bind(object obj)
        {
            //this.market.ItemsSource = this.connector.Market;

            this.bids.DataSource = this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Bids;
            this.asks.DataSource = this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Asks;
            this.trades.DataSource = this.connector.Market.Find("EGS74081C018").Trades;

            this.bids.Sort(this.bids.Columns[0], ListSortDirection.Descending);
            this.asks.Sort(this.asks.Columns[0], ListSortDirection.Ascending);
            this.trades.Sort(this.trades.Columns[0], ListSortDirection.Descending);

            ((IList)this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Bids).RemoveAt(0);
            ((IList)this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Asks).RemoveAt(0);
            ((IList)this.connector.Market.Find("EGS74081C018").Trades).RemoveAt(0);
        }
    }
}
